package com.tpc.client.modules.core.contact.datasource;

import com.tpc.client.modules.core.contact.service.ContactService;
import com.tpc.client.modules.core.contact.service.ContactServiceAsync;
import com.tpc.dao.extend.Contact;
import com.tpc.client.systeme.core.datasource.GwtRpcDataSource;

import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;

import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.tpc.client.systeme.core.Systeme;

public class ContactListDS extends GwtRpcDataSource {

    String strurl = null;
    Systeme systeme = null;

    public ContactListDS(){
        
    }

    public ContactListDS(String strID) {
            setID(strID);
            DataSourceField fldId = new DataSourceField("contact_liste_id", FieldType.INTEGER);
            fldId.setPrimaryKey(Boolean.TRUE);
            fldId.setHidden(Boolean.TRUE);
            DataSourceField fldDescription = new DataSourceField("contact_liste_notes", FieldType.TEXT);
            DataSourceField fldMailto = new DataSourceField("contact_liste_mailto", FieldType.TEXT, " ",30);
            fldMailto.setHidden(Boolean.TRUE);
            DataSourceField fldMailtoIcon = new DataSourceField("mailtoIcon_contact", FieldType.IMAGE, " ",30);
            DataSourceField fldTitle = new DataSourceField("title_contact", FieldType.ENUM, "Titre",40);
            DataSourceField fldName = new DataSourceField("contact_liste_name", FieldType.TEXT, "Nom");
            DataSourceField fldOrganisation = new DataSourceField("contact_liste_organisation", FieldType.TEXT, "Organisation",150);
            DataSourceField fldPhone = new DataSourceField("contact_liste_phonebusiness", FieldType.TEXT, "Téléphone",130);
            DataSourceField fldMobile = new DataSourceField("contact_liste_phonemobile", FieldType.TEXT, "Cellulaire",130);

            setFields(fldId,fldDescription,fldMailto,fldMailtoIcon,fldTitle,fldName,fldOrganisation,fldPhone,fldMobile);

            strurl = Page.getAppDir();
            strurl = strurl.substring(0,strurl.length()-1);
            strurl = strurl.substring(strurl.lastIndexOf("/"));

    }

    public void setObjects(Systeme cacheParam) {
        systeme = cacheParam;
    }

    @Override
    protected void executeFetch (final String requestId, final DSRequest request, final DSResponse response) {
        if (DOM.getElementById("splashScreenText") != null) {
            DOM.getElementById("splashScreenText").setInnerHTML("contacts ...");
        }
        ContactServiceAsync service = GWT.create(ContactService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        endpoint.setServiceEntryPoint(strurl+"/com.tpc.contact/contactloader");
        service.loadList(new AsyncCallback<Contact[]> () {
            public void onFailure (Throwable caught) {
                systeme.handleServerException(caught);
            }
            public void onSuccess (Contact[] result) {
                int NbrResultat = 0;
                ListGridRecord[] list = null;
                if (result != null) {
                    list = new ListGridRecord[result.length];
                    NbrResultat = result.length;
                    for (int i = 0; i < list.length; i++) {
                        list[i] = new ListGridRecord();
                        list[i].setAttribute("contact_liste_name",result[i].getFullName());
                        list[i].setAttribute("contact_liste_notes",result[i].getDescription());
                        String mailtoIcon = "blank";
                        if (result[i].getEmail() != null) {
                            list[i].setAttribute("contact_liste_mailto", result[i].getEmail().getUrl());
                            if (result[i].getEmail().getUrl() != null) {
                                if (result[i].getEmail().getUrl().length() > 4) {
                                    mailtoIcon = "emailLink";
                                }
                            }
                        }
                        list[i].setAttribute("mailtoIcon_contact",mailtoIcon);
                        list[i].setAttribute("contact_liste_title", result[i].getTitle().getName());
                        if (result[i].getOrganisation() != null) {
                            list[i].setAttribute("contact_liste_organisation", result[i].getOrganisation().getName());
                        }
                        if (result[i].getPhoneBusiness() != null) {
                            list[i].setAttribute("contact_liste_phonebusiness", result[i].getPhoneBusiness().getAreacode()+"-"+result[i].getPhoneBusiness().getNumber());
                        }
                        if (result[i].getPhoneMobile() != null) {
                            list[i].setAttribute("contact_liste_phonemobile", result[i].getPhoneMobile().getAreacode()+"-"+result[i].getPhoneMobile().getNumber());
                        }
                        list[i].setAttribute("contact_liste_id",result[i].getId());
                    }
                }
                systeme.getAppBar().getTab("CONTACTS").setTitle("Usagers ("+NbrResultat+")");
                response.setData(list);
                processResponse(requestId, response);
                systeme.hideSplashScreen();
            }
        });
    }

    @Override
    protected void executeAdd (final String requestId, final DSRequest request, final DSResponse response) {
        //@TODO : fdsa
    }

    @Override
    protected void executeUpdate (final String requestId, final DSRequest request, final DSResponse response) {
         //@TODO : fdsa
    }

    @Override
    protected void executeRemove (final String requestId, final DSRequest request, final DSResponse response) {
         //@TODO : fdsa
    }

}